//
//  GameController.m
//  Test01
//
//  Created by Storm Christoph on 11/27/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GameController.h"


@implementation GameController

@synthesize nrOfCards;
@synthesize nrOfCardsPerGroup;
@synthesize currentTime;

int groupCounter;			// Count the number of found groups to know whether game is over.
int secondsToRun;
BOOL timerStarted = NO;
BOOL timerPause = NO;

int nrOfRoundsPlayed = 0;
int timeReduction = 5;		// reduce X seconds per game
int timePerRound = 0;		// Time available for one round

NSMutableArray *flippedCards = nil;
MainViewController *viewController = nil;
NSTimer *timer = nil;


- (GameController *)initWithParameters:(int)nrCards group:(int)cardsPerGroup view:(id *)vc timer:(int)seconds
{
	[self initParameters:nrCards group:cardsPerGroup timer:seconds];
	
	viewController = (MainViewController *)vc;
	flippedCards = [[NSMutableArray alloc] initWithCapacity:nrOfCardsPerGroup];
	return self;
}

- (void) initParameters:(int)nrCards group:(int)cardsPerGroup timer:(int)seconds
{
	self.nrOfCards = nrCards;
	self.nrOfCardsPerGroup = cardsPerGroup;

	groupCounter = 0;
	secondsToRun = seconds - (nrOfRoundsPlayed * timeReduction);
	timePerRound = secondsToRun;
	
	NSLog(@"seconds to run %d, %d, %d", secondsToRun, nrOfRoundsPlayed, timeReduction);
}


- (BOOL)cardShouldFlip:(Card *) card
{	
	if (!timerStarted)
	{
		// start timer when user clicked first image
		[self startTimer:secondsToRun];
	}
	[self resumeTimer];
	
	if ([card isFlipped]) {
		return NO;
	}
	if ([flippedCards count] < nrOfCardsPerGroup) {
		[flippedCards addObject:card];
		card.isFlipped = YES;
		return YES;
	}
	
	return NO;
}

-(BOOL) checkGroupFound
{
	if ([flippedCards count] != nrOfCardsPerGroup) {
		return NO;
	}
	
	BOOL found = YES;
	for (int i = 0; i < [flippedCards count]-1; i++) {
		if (((Card*)[flippedCards objectAtIndex:i]).groupID != ((Card*)[flippedCards objectAtIndex:i+1]).groupID) {
			
			found = NO;
			break;
		}
	}
	
	if (!found) {
		for (int i = 0; i < [flippedCards count]; i++) {
			[((Card*)[flippedCards objectAtIndex:i]) doFlip];
		}
	} else {
		// hier könnten die gefundenen bilder gelöscht werden
		// überlegen ob wir das so machen oder verschieben oder ...
		groupCounter++;
		
		if (groupCounter == (nrOfCards / nrOfCardsPerGroup)) {
			// game solved
			NSLog(@"Game solved within time: %d!", secondsToRun);
			
			[self stopTimer];
			nrOfRoundsPlayed++;
			
			[viewController onGameSolved:(timePerRound - secondsToRun) round: nrOfRoundsPlayed];
			
		}
	}
	[flippedCards removeAllObjects];
	return YES;
}

-(void) startTimer:(int) timeToRun
{
	timerStarted = YES;
	
	if (timer == nil) {
		NSLog(@"start timer");
		// repeats:no => timer will only run once and does not repeat
		timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
	}
}

-(void) stopTimer
{
	timerStarted = NO;
	
	if(timer == nil)
	{
		NSLog(@"stop timer - timer is null");
		return;
	}
	
	NSLog(@"stop timer");
		
	[timer invalidate];
	timer = nil;
}

- (void) pauseTimer
{
	timerPause = YES;
}

- (void)resumeTimer
{
	timerPause = NO;
}

-(void) onTimer
{
	if (!timerPause) {
		secondsToRun--;
		
		CGFloat f = ((CGFloat)secondsToRun/(CGFloat)timePerRound);
		NSLog(@"%f = %d / %d",f,secondsToRun,timePerRound);
		
		[viewController updateTimerBar:f];
	}

	if (secondsToRun <= 0 && !timerPause) {
		[self pauseTimer];
		[viewController onTimer];
	}
}

-(int) showCurrentTime
{
	return -1; 
}


- (void)dealloc {
	[super dealloc];
}

@end
